package com.baizhi.controller;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.User;
import com.baizhi.entity.UserQuery;
import com.baizhi.service.UserService;
import com.baizhi.util.ThreadPollUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @LogAnnotation(value = "查询所有的用户",type = "selectpage")
    @ResponseBody
    @RequestMapping("selectPage")
    public Map selectPage(Integer page, Integer limit){
        Map m = new HashMap();
        PageInfo<User> pageInfo = userService.selectAll(page, limit);
        m.put("code",0);
        m.put("msg","");
        m.put("count",pageInfo.getTotal());
        m.put("data",pageInfo.getList());
        return m;
    }
    @ResponseBody
    @RequestMapping("selectOne")
    public User selectOne(Integer userId){
        User user = userService.selectByPrimaryKey(userId);

        return user;
    }
    @ResponseBody
    @RequestMapping("update")
    public Map update(User user){
        Map m = new HashMap();
        try {
            userService.updateStatus(user);
            m.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("flag",false);
        }
        return m;
    }
    @ResponseBody
    @RequestMapping("selectCount")
    public Integer[] selectCount(){
        List<User> users = userService.selectCount();
        Integer[] a = {users.get(0).getCount(),users.get(1).getCount()};
        return a;
    }
    @ResponseBody
    @RequestMapping("selectQuery")
    public List<UserQuery> selectQuery(){
        List<UserQuery> userQueries = userService.selectQuery();
        return userQueries;
    }
//    @ResponseBody
//    @RequestMapping("selectCountDate")
//    public Integer[] selectCountDate(){
//        User user = userService.selectCountDate(7);
//        Integer a = user.getCountDate();
//
//        User user1 = userService.selectCountDate(14);
//        Integer a1 = user1.getCountDate();
//
//        User user2 = userService.selectCountDate(21);
//        Integer a2 = user2.getCountDate();
//        Integer[] b = {a,a1-a,a2-a1};
//        return b;
//    }
    @ResponseBody
    @RequestMapping("selectCountDate1")
    public List<Map> selectCountDate1(){
        List<Map> maps = userService.selectCountDate1();
        return maps;
    }

    @ResponseBody
    @RequestMapping("selectCountAll")
    public Map selectCountAll(){
        long start = System.currentTimeMillis();
        Map m = new HashMap();
        List<Map> maps = userService.selectCountDate1();
        m.put("count",maps);

        List<UserQuery> userQueries = userService.selectQuery();
        m.put("province",userQueries);

        List<User> users = userService.selectCount();
        Integer[] a = {users.get(1).getCount(),users.get(0).getCount()};
        m.put("sex",a);
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms-----");
        return m;
    }

    @ResponseBody
    @RequestMapping("selectCountAllThread")
    @AddCacheAnnotation
    public Map selectCountAllThread(){
        long start = System.currentTimeMillis();
        Map m = new HashMap();
//        创建计数器 设置为3
        CountDownLatch countDownLatch = new CountDownLatch(3);
            /*
            创建3个子线程

            submit 提交一个任务给线程池，线程池会自动分配一个线程执行该任务
            Runnable 任务对象
            */
        ThreadPollUtil.executor.submit(new Runnable(){
            @Override
            public void run() {
//              执行任务一的查询
                m.put("count",userService.selectCountDate1());
//              计数器减1
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                m.put("province",userService.selectQuery());
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                List<User> users = userService.selectCount();
                Integer[] a = {users.get(1).getCount(),users.get(0).getCount()};
                m.put("sex",a);
                countDownLatch.countDown();
            }
        });

//        3.通过CountDownLatch让主线程等待
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start+"ms-----");
        return m;
    }
}
